var lnk_prods_opt = {
    showDialog : function(){
        easyDialog.open({
            container : 'lnk_prods_list',
            fixed : true
        });
		$.ajax({
			    "url" : "zbar2.html",
				"data" :  {action:'land_search'},
				"dataType":"json",
				"type": 'POST', 
				"success":function(data){
					var contents="";
					var list = data.list;
						for(var i in list){
						var id =list[i]["id"];
						var keywords =list[i]["ltitle"];
						contents=contents+"<li title="+keywords+" data-id="+id+" class=\"heihei\">"+keywords+"</li>";
						}
						$(".order_ul_a").html(contents); 
					}          
			});
			$(".heihei").live("click",function(){
				var  tpl = $("#list_template").html();
				$("#selected_lnk_prods").empty();
					var id = $(this).attr("data-id");
					var k = $(this).text();
					var lig = $(".ul_list li").length;
					if(lig>9){
						alert("最多只能添加十个");
						return false;
					}
					else{
						if(lnk_prods_opt.check(id)){
							  $("#selected_lnk_prods").after(template(tpl, {id:id,title:k}));
						}
						 else{
							alert("您已添加了同样的产品")
						 }	
					}
			})
			
		$("#mask").css("display","block")
    },
	check : function(id){
		var re = true;
		 $(".ul_list li input").each(function(){
			var data = $(this).val();
				if(id==data){
					re = false;
				}
		})
		return re;
	},
    closeDialog: function(){
        easyDialog.close();
		 location.reload();
    },
    rm : function(){
        $(this).parent().remove();
        var v = $(this).next('input').val();
        $("input[name='lnkprods'][value='"+v+"']").removeAttr('checked');
    },
    submit: function(){
		$("#t").submit();
        easyDialog.close();
    }
};
jQuery(document).ready(function($){
    $(".order_cc").on('click', lnk_prods_opt.showDialog);
    $("#selected_lnk_prods").on('click', 'a.rm', lnk_prods_opt.rm);
    $("#lnk_prods_cancel").on('click', lnk_prods_opt.closeDialog);
    $("#lnk_prods_ok").on('click', lnk_prods_opt.submit);
});

	$(document).ready(function(){ 
		if($("#ztime").val()=="4"){
		$(".time_none").css("display","block");
		}
		$.ajax({
			    "url" : "zbar2.html",
				"data" :  {action:'land_search'},
				"dataType":"json",
				"type": 'POST', 
				"success":function(data){
					var contents="";
					var list = data.list;
						for(var i in list){
						var keywords =list[i]["ltitle"];
						contents=contents+"<li title="+keywords+">"+keywords+"</li>";
						}
						$(".order_ul").html(contents); 
					
					}          
			});
        })
	$(".suggest_input").keyup(function(){
			if($(".suggest_input").val().length<=0){
				$(".liid").val("");
				$(".suggest_ul").hide(0);
				return; 
			}
			$.ajax({
			    "url" : "zbar2.html",
				"data" :  {action:'land_search',ltitle:$(".suggest_input").val()},
				"dataType":"json",
				"type": 'POST',   
				"success":function(data){
					$(".suggest_ul").show(0);
					var contents="";
					var list = data.list;
						for(var i in list){
						var id =list[i]["id"];
						var keywords =list[i]["ltitle"];
						contents=contents+"<li title="+keywords+" data-id="+id+" data-k="+keywords+">"+keywords+"</li>";
						}
						$(".suggest_ul").html(contents); 
					}    
			});
		  
	});  
	$(".suggest_ul").on("click","li",function(e){
				var target = $(e.currentTarget);
				var id = target.attr("data-id");
				var k = target.attr("data-k");
				var tpl = $("#list_template").html();
				$("#selected_lnk_prods").empty();
				//$(".suggest_input").val(target.text());
				$(".suggest_input").val("");
				//$("#selected_lnk_prods").after(template(tpl, {id:id, title:k}));
				$(".suggest_ul").hide(0);
				var lig = $(".ul_list li").length;
					if(lig>9){
						alert("最多只能添加十个");
						return false;
					}
					else{
						if(lnk_prods_opt.check(id)){
							  $("#selected_lnk_prods").after(template(tpl, {id:id,title:k}));
						}
						 else{
							alert("您已添加了同样的产品")
						 }	
					}
			})
		$(".rm").live('click',function(e){
				$(this).parent().remove();
				var v = $(this).next('input').val();
				$("input[name='lnkprods'][value='"+v+"']").removeAttr('checked');
			})

		function template(tpl, val){
				return tpl.replace(/\{(\w+)\}/g, function(m,i){
					if(val[i]) {
						return val[i];
					} else {
						return '';
					}
				})
			};
	


